const app = getApp()
var login_id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ketizu: [],
    ketizuIndex: 0,
    userInfo:{},
    ketizuid:'',
    id:0,
    school:'',
    avatar:'',
    flag:false,
    hasUserInfo:false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  goLogin:function(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  logout:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定退出当前账号？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定” 
      confirmColor: 'skyblue', //确定文字的颜色
      success: function (res) {
        if (!res.cancel) {
          wx.setStorageSync('user', "");
          that.setData({
            flag: false
          })
        }
      },
      fail: function (res) { }, //接口调用失败的回调函数
      complete: function (res) { }, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  jumpSchools:function(){
    wx.navigateTo({
      url: '/pages/school/school',
    })
    
  },
  getKeTizu:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl + 'IS00/Login.ashx?Method=GetKeTiZuList&LoginID=' + login_id,
      method: "GET",
      data: {},
      success: res => {
        if (res.data.state == 1) {          
          for (var i = 0; i < res.data.retData.KeTiZuList.length; i++) {
            res.data.retData.KeTiZuList[i].id =i;
          }   
          wx.setStorageSync('aaa', res.data.retData.KeTiZuList[0].KeTiZuID);       
          that.setData({
            ketizu: res.data.retData.KeTiZuList,
            ketizuid : res.data.retData.KeTiZuList[0].KeTiZuID,
          })
        }
      }
    })
  },
  bindXmChange: function (e) {
    var that = this
    var ketizu = that.data.ketizu
    that.setData({
      id: e.detail.value
    })
    for (var i = 0; i < ketizu.length;i++){
      if (ketizu[i].id == e.detail.value){
        console.log(ketizu[i].id)
        wx.setStorageSync('aaa', ketizu[i].KeTiZuID);    
      }
    }
    
    // var login_id = app.globalData.login_id
    that.getIndex(login_id);
  },
  jumpList :res=>{
    wx.navigateTo({
      url: '/pages/list_yqyy/list_yqyy?cateid=' + res.currentTarget.dataset.catid
    })
  },
  jumpListSYS: res => {
    wx.navigateTo({
      url: '/pages/list_sysyy/list_sysyy?cateid=' + res.currentTarget.dataset.catid
    })
  },
  goAllYQ:res=>{
    wx.navigateTo({
      url: '/pages/list_yqyy/list_yqyy?cateid=0',
    })
  },
  goAllSYS: res => {
    wx.navigateTo({
      url: '/pages/list_sysyy/list_sysyy?cateid=0',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
   
    


  },
  getUserInfo: function (e) {
    if (e.detail.userInfo) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }

  },
  /***
   * 首页信息
   */
  getIndex(login_id){
    var that = this
    that.getKeTizu()
    var ketizu_id = wx.getStorageSync('aaa');
    wx.request({
      url: app.globalData.apiUrl + 'IS00/Login.ashx?Method=GetUserInfo&LoginID=' + login_id + '&KeTiZuID=' + ketizu_id,
      method: "GET",
      data: {},
      success: res=> {
        if (res.data.state == 1) {
          var arr = res.data.retData.JiBenXinXi.split('|');
          var arr_res = []
          for(var i=0;i<arr.length ;i++){
            arr_res[i] = arr[i].split(':');
          }
  
          that.setData({
            username: res.data.retData.XingMing,
            DaiShenHeNumSYS: res.data.retData.DaiShenHeNumSYS,
            DaiShenHeNumYQ: res.data.retData.DaiShenHeNumYQ,
            WeiGuoNumSYS: res.data.retData.WeiGuoNumSYS,
            WeiGuoNumYQ: res.data.retData.WeiGuoNumYQ,
            YiShenHeNumSYS: res.data.retData.YiShenHeNumSYS,
            YiShenHeNumYQ: res.data.retData.YiShenHeNumYQ,
            arr_res: arr_res,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { 

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    
    if (wx.getStorageSync("userInfo")) {
      this.setData({
        userInfo: wx.getStorageSync("userInfo"),
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
       
      app.getuserInfoReadyCallback = res => {
        this.setData({
          userInfo: wx.getStorageSync("userInfo"),
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          wx.setStorageSync("userInfo", res.userInfo);
          this.setData({
            userInfo: wx.getStorageSync("userInfo"),
            hasUserInfo: true
          })
        }
      })
    }

    login_id = wx.getStorageSync('user') == "" ? -999 : wx.getStorageSync('user').UserID;
    if (login_id != -999) {
      that.setData({
        flag: true
      })
    }
    that.getIndex(login_id);   
    
    this.setData({
      school: wx.getStorageSync('school').name
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }














})